######################################
## Battery Pipe Menu                ##
## v 1.0                            ##
## v 1.1                            ##
##      by jpope 01.18.2010         ##
##                                  ##
## tested on Asus EeePC 1000HE(B)   ##
##                                  ##
######################################

## Settings
batt=$(echo BAT0)

## Data
batt_OEM=$(cat /proc/acpi/battery/"$batt"/info | grep 'OEM' | cut -c 26-)
batt_Model=$(cat /proc/acpi/battery/"$batt"/info | grep 'model' | cut -c 26-)
batt_type=$(cat /proc/acpi/battery/"$batt"/info | grep 'type' | cut -c 26-)

batt_desncap=$(cat /proc/acpi/battery/"$batt"/info | grep 'design capacity:')
batt_lastfull=$(cat /proc/acpi/battery/"$batt"/info | grep 'last')
batt_desnvolt=$(cat /proc/acpi/battery/"$batt"/info | grep 'design v')
batt_warn=$(cat /proc/acpi/battery/"$batt"/info | grep 'design capacity w')
batt_low=$(cat /proc/acpi/battery/"$batt"/info | grep 'design capacity l')

batt_state1=$(cat /proc/acpi/battery/"$batt"/state | grep 'capacity state' | cut -c 26-)
batt_state2=$(cat /proc/acpi/battery/"$batt"/state | grep 'ch' | cut -c 26-)
batt_rate=$(cat /proc/acpi/battery/"$batt"/state | grep 'present r')
batt_volt=$(cat /proc/acpi/battery/"$batt"/state | grep 'present v')
batt_cap=$(cat /proc/acpi/battery/"$batt"/state | grep 'remain')

batt_r=$(cat /proc/acpi/battery/"$batt"/state | grep 'remain' | cut -c 26-29)
batt_f=$(cat /proc/acpi/battery/"$batt"/info | grep 'last' | cut -c 26-29)
batt_l=$(cat /proc/acpi/battery/"$batt"/info | grep 'design capacity l' | cut -c 26-29)
batt_ra=$(cat /proc/acpi/battery/"$batt"/state | grep 'present r' | cut -c 26-29)

batt_perc=$(echo "scale=4; ($batt_r/$batt_f)*100" | bc | cut -c -5)
batt_time_c=$(echo "scale=4; ($batt_f-$batt_r)/$batt_ra" | bc | cut -c -4)
batt_time_d=$(echo "scale=4; ($batt_r-$batt_l)/$batt_ra" | bc | cut -c -4)
batt_time=$(case $(echo $batt_state2 | cut -c -7) in
                dischar) echo "approx time left: $batt_time_d hours" ;;
                chargin) echo "approx time to full: $batt_time_c hours" ;;
                charged) echo "full charge / on ac" ;;
                *) echo "battery time N/A" 
            esac)
            
## Pipemenu
    echo "<openbox_pipe_menu>"
        echo "<separator label=\"$batt_OEM  $batt_Model $batt_type $batt Info\"/>"
        echo "<item label=\"state:  $batt_perc% $batt_state1 $batt_state2 \"/>"
        echo "<separator/>"
            echo "<item label=\"$batt_time\"/>"
            echo "<item label=\"$batt_rate\"/>"
            echo "<item label=\"$batt_cap\"/>"
        echo "<separator/>"
            echo "<item label=\"$batt_lastfull\"/>"
            echo "<item label=\"$batt_desncap\"/>"
            echo "<item label=\"$batt_warn\"/>"
            echo "<item label=\"$batt_low\"/>"
        echo "<separator/>"
            echo "<item label=\"$batt_volt\"/>"
            echo "<item label=\"$batt_desnvolt\"/>"
        echo "<separator/>"
            echo "<item label=\"Open Power Settings                 -->\">"
		        echo "<action name=\"Execute\">"
		            echo "<execute>"
		                echo "gnome-power-preferences"
		            echo "</execute>"
		        echo "</action>"
		    echo "</item>"
		    echo "<item label=\"Open Power History                   -->\">"
		        echo "<action name=\"Execute\">"
		            echo "<execute>"
		                echo "gnome-power-statistics"
		            echo "</execute>"
		        echo "</action>"
		    echo "</item>"
    echo "</openbox_pipe_menu>"